package approve.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import approve.action.App_ApprovedDocument;
import approve.action.R;
import approve.model.Approvedoc;

public class ApprovedDocLibraryAdapter extends BaseAdapter {

	App_ApprovedDocument context;
	private List<Approvedoc> lstApp;
	List<Approvedoc> listcheck = new ArrayList<Approvedoc>();
	int screenwidth;
	String lable;
	LinearLayout flag = null;
	String strid="";
	int flagpage;
	ViewHolder holder;
	public ApprovedDocLibraryAdapter(App_ApprovedDocument context,List<Approvedoc> lstApp,int screenwidth,String lable){
		this.context = context;
		this.lstApp = lstApp;
		this.screenwidth= screenwidth;
		this.lable=lable;
	}
	@Override
	public int getCount() {
		return lstApp.size();
	}

	@Override
	public Object getItem(int arg0) {
		return lstApp.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	
	public List<Approvedoc> getListcheck() {
		return listcheck;
	}

	public void setListcheck(List<Approvedoc> listcheck) {
		this.listcheck = listcheck;
	}

	
	public String getStrid() {
		strid="";
		if(listcheck.size()>0){
			for(int i=0;i<listcheck.size();i++){
				if(strid.equals("")){
					strid="'"+listcheck.get(i).getiDGroupApprove()+"'";
				}else{
					strid+=",'"+listcheck.get(i).getiDGroupApprove()+"'";
				}
			}
		}
		return strid;
	}

	public void setStrid(String strid) {
		this.strid = strid;
	}

	@Override
	public View getView(final int position, View convertview, ViewGroup parent) {
		 final Approvedoc doc = lstApp.get(position);
		 
		if(convertview==null){
			holder = new ViewHolder();
			
			LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertview=inflater.inflate(R.layout.approveddoclibraryadapter, parent,false);
			
			holder.textView = (TextView) convertview.findViewById(R.id.labelListApp);
			holder.textViewNew = (TextView)convertview.findViewById(R.id.textViewNew);
			holder.check =(CheckBox)convertview.findViewById(R.id.checklibrary);
			holder.textdate = (TextView) convertview.findViewById(R.id.labeldate);
			holder.lstAppr = (LinearLayout)convertview.findViewById(R.id.lstAppr_library);
			convertview.setBackgroundResource(R.drawable.list_selector);
			convertview.setTag(holder);
		}else{
			holder = (ViewHolder)convertview.getTag();
		}
		
		if(listcheck.contains(doc)){
			holder.check.setChecked(true);
		}else{
			holder.check.setChecked(false);
		}

		holder.check.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(!listcheck.contains(doc)){
					listcheck.add(doc);
				}else{
					listcheck.remove(doc);
				}
			}
		});
		
		holder.textViewNew.setVisibility(View.GONE);
		holder.textView.setText(doc.getTitle());

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(doc.getTimeApprove()!=null && !doc.getTimeApprove().equals(null) && !doc.getTimeApprove().equals("")){
			holder.textdate.setText(lable+": "+df.format(doc.getTimeApprove()));	
		}else{
			holder.textdate.setText(lable+": ");
		}
		
		holder.lstAppr.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				((App_ApprovedDocument)context).getAlertwaiting1().show();
				try{
					new Thread(){
						public void run() {
							((App_ApprovedDocument)context).onLoadApp(doc);
							((App_ApprovedDocument)context).getUiHandler1().post( new Runnable(){
								@Override
								public void run() {
									if(((App_ApprovedDocument)context).isUpdateRequired1()){
									}else{
										((App_ApprovedDocument)context).getAlertwaiting1().cancel();
									}
								}
							} );
						}
					}.start();
				}catch (Exception e) {}
			}
		});

	
		return convertview;
	}

	public static class ViewHolder {
        TextView textView,textViewNew,textdate;
        CheckBox check;
        LinearLayout lstAppr;
    }
}
